package com.demo.index;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;



import java.util.List;

@ControllerBind(controllerKey = "/")
public class IndexController extends Controller {
	public static String CN_NAME = "XM";

	
    @ClearInterceptor(ClearLayer.ALL)
    public void index() {
        String user = getSessionAttr(CN_NAME);
        if (user == null) {
            render("/index/login.html");
        } else {
          
                render("/index/index.html");
        }
    }

    @Before(POST.class)
    public void login() {
        String userName = getPara("userName");
        String passwd = getPara("passwd");
        String page="";

            if (StringKit.notBlank(userName) && StringKit.notBlank(passwd)) {
        		System.out.println("============"+userName);

            	if(userName.equals("admin") && passwd.equals("admin123")){
            		   setSessionAttr(CN_NAME, userName);
                       page="/index/index.html" ;
            	}  else {
                    setAttr("msg", "登陆失败 ！用户名或者密码错误！");
                	page="/index/login.html" ;
            	}
            } else {
                setAttr("msg", "登陆失败 ！用户名或者密码错误！");
            	page="/index/login.html" ;
            }

        render(page);
//        render("mobile/index.html") ;
    }

    @ClearInterceptor(ClearLayer.ALL)
    public void logout() {
        String user = getSessionAttr(CN_NAME);
        if (user != null) {
            removeSessionAttr(CN_NAME);
        }
        render("/index/login.html");
    }
   
}
